using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;

public class HttpUtility : Singleton<HttpUtility>
{
    public int Timeout { get; set; } = 15;
    /// <summary>
    /// ?????????? HTTP GET ????
    /// </summary>
    /// <param name="url"></param>
    /// <param name="cb"></param>
    public void Get(string url, Dictionary<string, string> headers, Action<UnityWebRequest> cb)
    {
        Loom.Current.StartCoroutine(get(url, headers, cb));
    }
    private IEnumerator get(string url, Dictionary<string, string> headers, Action<UnityWebRequest> cb)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        if (headers != null)
        {
            foreach (var pair in headers)
                request.SetRequestHeader(pair.Key, pair.Value);
        }
        request.timeout = Timeout;
        yield return request.SendWebRequest();
        cb(request);
        request.Dispose();
    }

    /// <summary>
    /// ?????????? HTTP POST ????
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <param name="cb"></param>
    public void Post(string url, Dictionary<string, string> headers, byte[] data, Action<UnityWebRequest> cb)
    {
        Loom.Current.StartCoroutine(post(url, headers, data, cb));
    }
    private IEnumerator post(string url, Dictionary<string, string> headers, byte[] data, Action<UnityWebRequest> cb)
    {
        UnityWebRequest request = new UnityWebRequest(url, "POST");
        request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //request.SetRequestHeader("Content-Type", "application/json");
        
        if (headers != null)
        {
            foreach (var pair in headers)
                request.SetRequestHeader(pair.Key, pair.Value);
        }
        request.uploadHandler = new UploadHandlerRaw(data);
        request.downloadHandler = new DownloadHandlerBuffer();
        request.timeout = Timeout;
        yield return request.SendWebRequest();

        cb(request);
        request.Dispose();
    }

    /// <summary>
    /// ???????????????????
    /// </summary>
    /// <param name="url"></param>
    /// <param name="cb"></param>
    public void GetTexture(string url, Action<UnityWebRequest> cb)
    {
        Loom.Current.StartCoroutine(getTexture(url, cb));
    }
    protected IEnumerator getTexture(string url, Action<UnityWebRequest> cb)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.downloadHandler = new DownloadHandlerTexture();
        request.timeout = Timeout;
        yield return request.SendWebRequest();

        cb(request);
        request.Dispose();
    }

    private string PathToUrl(string path)
    {
        if (string.IsNullOrEmpty(path) || path.StartsWith("jar:file://") || path.StartsWith("file://") || path.StartsWith("http://") || path.StartsWith("https://"))
        {
            return path;
        }
        if (Application.platform == RuntimePlatform.OSXEditor ||
            Application.platform == RuntimePlatform.OSXPlayer ||
            Application.platform == RuntimePlatform.IPhonePlayer ||
            Application.platform == RuntimePlatform.Android)
        {
            path = "file://" + path;
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
        {
            path = "file:///" + path;
        }
        return path;
    }
}
